package interactiond

import (
	"fmt"

	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

const (
	// Group 互动组标识
	Group = "group"
)

var (
	_ GroupAggregateRoot = &groupAggregateRoot{}
)

func initGroup(d ddd.Domain, repo GroupRepo) {
	car := &groupAggregateRoot{}
	car.Init(car, d, Group)
	repo.Init(repo, car, Group)
}

// GroupAggregateRoot 互动组聚合根
type GroupAggregateRoot interface {
	ddd.AggregateRoot
	ID(targettype IGTargetType, targetid string) string
	CreGroup(ctx ddd.Context, desc GroupDesc, ctrl GroupCtrl) (id string, err error)
	AddReplayNum(ctx ddd.Context, igid string, t ChildReplyTotal) (err error)
	// // 针对组进行评论
	// // 发出添加评论事件
	// Replay(ctx ddd.Context, targettype IGTargetType, targetid string, desc ReplayDesc) (id string, err error)

	// 通过指定的组id获取互动组聚合
	GetGroup(ctx ddd.Context, id string) (*GroupAggregate, error)

	// 通过指定的目标id和目标类型获取互动组聚合
	GetIGroup2(ctx ddd.Context, targettype IGTargetType, targetid string) (*GroupAggregate, error)
	AddIntsNum(ctx ddd.Context, igid string, code string, value int) (err error)

	QueryByIDS(ctx ddd.Context, igids []string) ([]GroupAggregate, error)
	// // 互动交互
	// Interaction(ctx ddd.Context, targettype IGTargetType, targetid string, uid string, t TypeAggregate, score int) (err error)
}

// GroupRepo 互动组仓储
type GroupRepo interface {
	ddd.Repository
	// // 针对组进行评论
	// // 发出添加评论事件
	// Replay(ctx ddd.Context, path ReplayPath, desc ReplayDesc) (id string, err error)
	// 通过指定的组id获取互动组聚合
	GetGroup(ctx ddd.Context, id string) (*GroupAggregate, error)
	// // Interaction 互动
	// // desc 互动内容
	// // mode 互动模式
	// Interaction(ctx ddd.Context, mode ITMode, desc InteractionDesc) (err error)

	//	QueryInteractionPage(ctx ddd.Context, igid string, code []string, pi common.PageInfo) (pr common.PageResult, result *[]InteractionAggregate, err error)
	AddReplayNum(ctx ddd.Context, igid string, t ChildReplyTotal) (err error)
	CreGroup(ctx ddd.Context, id string, desc GroupDesc, ctrl GroupCtrl) (err error)

	AddIntsNum(ctx ddd.Context, igid string, code string, value int) (err error)
	SubIntsNum(ctx ddd.Context, igid string, code string, value int) (err error)
	UpdateIntsNum(ctx ddd.Context, igid string, code string, old int, new int) (err error)

	UpdateGroupCtrl(ctx ddd.Context, igid string, ctrl GroupCtrl) (err error)

	QueryByIDS(ctx ddd.Context, igids []string) ([]GroupAggregate, error)
}

// GroupAggregate 互动组聚合
type GroupAggregate struct {
	ddd.BaseAggregate
	IGID string `json:"igid"` // 互动组编号

	GroupDesc       `json:"desc"`       // 互动组描述实例
	ChildReplyTotal `json:"replytotal"` // 子评论信息汇总
	GroupCtrl       `json:"ctrl"`       // 互动组控制值对象
	GroupTotal      []GroupTotal        `json:"total"` // 互动汇总
}

// Repo 获取对应的仓储
func (a *GroupAggregate) Repo() GroupRepo {
	return a.Repository().(GroupRepo)
}

// AddReplayNum 增加评论统计
func (a *GroupAggregate) AddReplayNum(ctx ddd.Context, t ChildReplyTotal) (err error) {
	return a.Repo().AddReplayNum(ctx, a.IGID, t)
}

// AddIntsNum 增加互动统计
func (a *GroupAggregate) AddIntsNum(ctx ddd.Context, code string, value int) (err error) {
	return a.Repo().AddIntsNum(ctx, a.IGID, code, value)
}

// SubIntsNum 撤销互动同级
func (a *GroupAggregate) SubIntsNum(ctx ddd.Context, code string, value int) (err error) {
	return a.Repo().SubIntsNum(ctx, a.IGID, code, value)
}

// UpdateIntsNum 更新互动统计
func (a *GroupAggregate) UpdateIntsNum(ctx ddd.Context, code string, old int, new int) (err error) {
	return a.Repo().UpdateIntsNum(ctx, a.IGID, code, old, new)
}

// UpdateGroupCtrl 更新互动组控制信息
func (a *GroupAggregate) UpdateGroupCtrl(ctx ddd.Context, ctrl GroupCtrl) (err error) {
	return a.Repo().UpdateGroupCtrl(ctx, a.IGID, ctrl)
}

// // Replay 针对组进行评论
// // 发出添加评论事件
// func (a *GroupAggregate) Replay(ctx ddd.Context, desc ReplayDesc) (id string, err error) {

// 	path := ReplayPath{ParentID: a.IGID}
// 	id, err = a.Repo().Replay(ctx, path, desc)
// 	if err == nil {
// 		a.Trigger(EvtTypType, EvtActReplay, path)
// 	}
// 	return
// }

// // Interaction 互动
// // uid 互动人
// // code 互动类型编码
// // score 评价分数
// // 如果互动成功发出互动成功事件
// // 如果score的值超出了最大值，则已最大值计算
// // 如果已经评分，分数相同则不修改，并不产生事件，也不产生错误
// func (a *GroupAggregate) Interaction(ctx ddd.Context, uid string, t *TypeAggregate, score int) (err error) {
// 	i := InteractionDesc{IGID: a.IGID, UID: uid, Code: t.Code, Value: score}
// 	err = a.Repo().Interaction(ctx, t.Mode, i)
// 	if err == nil {
// 		a.Trigger(EvtTypGroup, EvtActInteraction, i)
// 	}
// 	return
// }

type groupAggregateRoot struct {
	ddd.BaseAggregateRoot
}

// Repo 获取对应的仓储
func (a *groupAggregateRoot) Repo() GroupRepo {
	return a.Repository().(GroupRepo)
}
func (a *groupAggregateRoot) ID(targettype IGTargetType, targetid string) string {
	return fmt.Sprintf("%03d-%v", targettype, targetid)
}

// 创建评论组
// 发出添加评论组事件
func (a *groupAggregateRoot) CreGroup(ctx ddd.Context, desc GroupDesc, ctrl GroupCtrl) (id string, err error) {
	id = a.ID(desc.TargetType, desc.TargetID)
	return id, a.Repo().CreGroup(ctx, id, desc, ctrl)
}
func (a *groupAggregateRoot) AddReplayNum(ctx ddd.Context, igid string, t ChildReplyTotal) (err error) {
	return a.Repo().AddReplayNum(ctx, igid, t)
}
func (a *groupAggregateRoot) AddIntsNum(ctx ddd.Context, igid string, code string, value int) (err error) {
	return a.Repo().AddIntsNum(ctx, igid, code, value)
}

// // 针对组进行评论
// // 发出添加评论事件
// func (a *groupAggregateRoot) Replay(ctx ddd.Context, targettype IGTargetType, targetid string, desc ReplayDesc) (id string, err error) {
// 	igid := a.id(targettype, targetid)
// 	if ga, err := a.GetGroup(ctx, id); err != nil {
// 		return "", err
// 	} else if ga == nil {

// 		return "", errors.New("组没有创建不能评论")
// 	}
// 	id, err = a.Repo().Replay(ctx, ReplayPath{IGID: igid}, desc)
// 	if err == nil {
// 		a.Trigger(EvtTypGroup, EvtActReplay, id)
// 	}
// 	return
// }

// 通过指定的组id获取互动组聚合
func (a *groupAggregateRoot) GetGroup(ctx ddd.Context, id string) (*GroupAggregate, error) {
	return a.Repo().GetGroup(ctx, id)
}

// 通过指定的目标id和目标类型获取互动组聚合
func (a *groupAggregateRoot) GetIGroup2(ctx ddd.Context, targettype IGTargetType, targetid string) (*GroupAggregate, error) {
	return a.Repo().GetGroup(ctx, a.ID(targettype, targetid))
}

func (a *groupAggregateRoot) QueryByIDS(ctx ddd.Context, igids []string) ([]GroupAggregate, error) {
	return a.Repo().QueryByIDS(ctx, igids)
}

// func (a *groupAggregateRoot) Interaction(ctx ddd.Context, targettype IGTargetType, targetid string, uid string, t TypeAggregate, score int) (err error) {
// 	igid := a.id(targettype, targetid)
// 	if ga, err := a.GetGroup(ctx, igid); err != nil {
// 		return err
// 	} else if ga == nil {
// 		if _, err := a.creGroup(ctx, GroupDesc{TargetID: targetid, TargetType: targettype}); err != nil {
// 			return err
// 		}
// 	}
// 	i := InteractionDesc{
// 		IGID:  igid,
// 		UID:   uid,
// 		Code:  t.Code,
// 		Value: score,
// 	}
// 	err = a.Repo().Interaction(ctx, t.Mode, i)
// 	if err == nil {
// 		a.Trigger(EvtTypGroup, EvtActInteraction, EvtActInteractionData{Data: i})
// 	}
// 	return
// }
